﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Data;
using System.Web;

namespace Tesla.Service.WcfServiceCommon
{
    public static class Config
    {
        static Configuration config = null;
        static string strconsoleMode = string.Empty;
        static Config()
        {
            
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

        public static string GetAppSetting(string name)
        {
            return GetAppSetting(name, false);
        }

        public static string GetAppSetting(string name, bool returnNameIfNotFound)
        {
            try
            {
                return config.AppSettings.Settings[name].Value;
            }
            catch (IndexOutOfRangeException)
            {
                if (returnNameIfNotFound)
                    return name;
                else
                    return null;
            }
        }

        static object lob = new object();
        static WcfServiceConfigurationSection wcfServices = null;
        static Dictionary<Type, ServiceConfig> wcfServiceConfigByType = new Dictionary<Type, ServiceConfig>();
        static Dictionary<string, ServiceConfig> wcfServiceConfigByKey = new Dictionary<string, ServiceConfig>();

        static void InitializeConfig()
        {
            
            string strEmployeeHostType = ConfigurationManager.AppSettings["employeeHostType"];
            string strEmployeeContractType = ConfigurationManager.AppSettings["employeeContractType"];
            string strWCFKey = ConfigurationManager.AppSettings["wcfKey"];
            string strWCFServiceAddressPort = ConfigurationManager.AppSettings["wcfServiceAddressPort"];
            string strWCFServiceEndPointName = ConfigurationManager.AppSettings["wcfServiceEndPointName"];
            string strWCFServiceAuthorizedGroups = ConfigurationManager.AppSettings["wcfServiceAuthorizedGroups"];
            strconsoleMode = ConfigurationManager.AppSettings["consoleMode"];

            
            WcfServiceConfigElement item = new WcfServiceConfigElement(strWCFKey, strWCFServiceAddressPort, strWCFServiceEndPointName, strWCFServiceAuthorizedGroups, strEmployeeHostType, strEmployeeContractType);
            
            Type hostType = Type.GetType(item.HostTypeDeclaration);
            Type contractType = Type.GetType(item.ContractTypeDeclaration);
            var configItem = new ServiceConfig { Item = item, HostType = hostType, ContractType = contractType };

            if (hostType != null && !wcfServiceConfigByType.ContainsKey(hostType))
                wcfServiceConfigByType.Add(hostType, configItem);

            if (contractType != null && !wcfServiceConfigByType.ContainsKey(contractType))
                wcfServiceConfigByType.Add(contractType, configItem);

            if (!wcfServiceConfigByKey.ContainsKey(item.Key))
                wcfServiceConfigByKey.Add(item.Key, configItem);
            
        }

        public static bool IsConsoleMode
        {
            get
            {
                //if (wcfServices == null) InitializeConfig();
                //return strconsoleMode.Trim() == "On" ? true : false;
                return true;
            }
        }

        public static ServiceConfig GetServiceConfig(Type type)
        {
            lock (lob)
            {
                if (wcfServices == null) InitializeConfig();
                if (wcfServiceConfigByType.ContainsKey(type))
                    return wcfServiceConfigByType[type];
                else
                    return null;
            }
        }

        public static ServiceConfig GetServiceConfig(string key)
        {
            lock (lob)
            {
                if (wcfServices == null) InitializeConfig();
                if (wcfServiceConfigByKey.ContainsKey(key))
                    return wcfServiceConfigByKey[key];
                else
                    return null;
            }
        }

        public static List<ServiceConfig> GetServiceConfigList()
        {
            lock (lob)
            {
                if (wcfServices == null) InitializeConfig();
                return wcfServiceConfigByKey.Values.ToList();
            }
        }
    }

    public class ServiceConfig
    {
        public Type HostType { get; set; }
        public Type ContractType { get; set; }
        public WcfServiceConfigElement Item { get; set; }
    }

}
